/*********************************************************
 * copyright xinbohit.com 版权所有 
 * 开发人员：IvanYu
 * 创建时间：2014/10/21 10:56:38
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity.Enum;
using LEARNREN.COM.UI;
using LEARNREN.COM.Entity;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.WebSite.Admin
{
    public partial class jobedit : BasePage
    {
        JobLogic jobLogic = null;
        CatalogueLogic catalogueLogc = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            BindDropdownlist();
            EditStateEnum nowAction = GetAction();
            if (!IsPostBack)
            {
                InitPageCtrl(nowAction);
            }
            if (nowAction == EditStateEnum.New)
            {
                this.Title = "添加职位";
            }
            else
            {
                this.Title = "修改职位";
            }
        }

        private void InitPageCtrl(EditStateEnum nowAction)
        {
            switch (nowAction)
            {
                case EditStateEnum.New:
                    ClearControlText(this.txtTitle, this.txtPlace, this.txtNum, this.txtDiscription);
                    break;
                case EditStateEnum.Edit:
                    string strId = Request.QueryString["id"];
                    int id = 0;
                    if (int.TryParse(strId, out id))
                    {
                        Job job = jobLogic.GetList(p => p.Id == id).FirstOrDefault();
                        if (job != null)
                        {
                            this.txtTitle.Text = job.Title;
                            this.txtDiscription.Text = job.Discription;
                            this.txtNum.Text = job.Num.ToString();
                            this.txtPlace.Text = job.Place;
                            this.SelectCatalogueDropDownList1.SelectedCatalogueId = job.CatalogueId;
                        }
                    }

                    break;
                default:
                    break;
            }
        }

        private void BindDropdownlist()
        {
            this.SelectCatalogueDropDownList1.CatalogueLogicProvider = catalogueLogc;
            this.SelectCatalogueDropDownList1.CanClickNodeType = CatalogueTypeEnum.JobList;
        }

        /// <summary>
        /// 保存职位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveJob_Click(object sender, EventArgs e)
        {
            try
            {
                Job job = GetEditingJob();
                jobLogic.Save(job);

                // 如果是添加则累加栏目内容数量
                EditStateEnum editEnum = GetAction();
                if (editEnum == EditStateEnum.New)
                {
                    catalogueLogc.IncreaseContentCount(job.CatalogueId);
                    AddScript("showDialog('职位添加成功。')");
                    ClearControlText(this.txtTitle, this.txtPlace, this.txtNum, this.txtDiscription);
                }
                else
                {
                    AddScript("showDialog('职位编辑成功。')");
                }
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                EditStateEnum editEnum = GetAction();
                if (editEnum == EditStateEnum.New)
                {
                    AddScript("showDialog('职位添加失败。')");
                }
                else
                {
                    AddScript("showDialog('职位编辑失败。')");
                }
            }
        }

        /// <summary>
        /// 获取正在编辑的职位对象，并同步数据库中关键字情况
        /// </summary>
        /// <returns></returns>
        private Job GetEditingJob()
        {
            Job job = null;

            EditStateEnum editEnum = GetAction();
            switch (editEnum)
            {
                case EditStateEnum.New:
                    job = new Job();
                    job.Id = jobLogic.GetNewIdentity();
                    job.CreatedTime = DateTime.Now;
                    break;
                case EditStateEnum.Edit:
                    string strId = Request.QueryString["id"];
                    int id = 0;
                    if (int.TryParse(strId, out id))
                    {
                        job = jobLogic.GetList(p => p.Id == id).FirstOrDefault();
                    }

                    break;
                default:
                    break;
            }


            // 为属性赋新值
            job.Title = this.txtTitle.Text;
            job.CatalogueId = this.SelectCatalogueDropDownList1.SelectedCatalogueId;
            job.ParentPath = this.SelectCatalogueDropDownList1.SelectedCataloguePath;
            job.Num = int.Parse(this.txtNum.Text);
            job.Place = txtPlace.Text;
            job.Discription = this.txtDiscription.Text;

            return job;
        }
        
        protected override void InitLogic()
        {
            jobLogic = new JobLogic();
            catalogueLogc = new CatalogueLogic();
        }

        protected override void DisposeLogic()
        {
            jobLogic.Dispose();
            jobLogic = null;
            catalogueLogc.Dispose();
            catalogueLogc = null;
        }
    }
}